package online_store.beanform;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import online_store.model.dto.ProductDTO;
import online_store.utils.Constants;

public class ActionForm {
	
//	public ProductDTO getEditProductForm(HttpServletRequest request){
//		ProductDTO product = new ProductDTO();
//		
//		
//		//process only if its multipart content
//		if(ServletFileUpload.isMultipartContent(request)){
//			try{
//				DiskFileItemFactory factory = new DiskFileItemFactory();
//				List<FileItem> multipart = new ServletFileUpload(factory).parseRequest(request);
//				
//				for(FileItem item : multipart){
//					if(!item.isFormField()){
//						String fileName = new File(item.getName()).getName();
//						item.write(new File(Constants.UPLOAD_DIRECTORY + File.separator + fileName));
//						product.setImage(fileName);
//					}else{
//						if(item.getFieldName().equals("name")){
//							product.setName(item.getString());
//						}else if(item.getFieldName().equals("desc")){
//							product.setDesc(item.getString());
//						}else if(item.getFieldName().equals("price")){
//							product.setPrice(Double.parseDouble(item.getString()));
//						}else if(item.getFieldName().equals("category")){
//							product.setCategory_id(Integer.parseInt(item.getString()));
//						}
//					}
//				}
//			}catch(Exception e){
//				System.out.println("Upload File Unsuccessfully!");
//				e.printStackTrace();
//			}
//		}else{
//			System.out.println("Sorry this Servlet only handles file upload request!");
//		}
//		product.setLast_update(new Date());
//	}
}
